package o2o.service.user;

import com.jinyou.utils.common.DateUtils;
import com.jinyou.utils.common.ValidateUtil;
import o2o.config.dao.UserDaoSql;
import o2o.config.dao.utils.DaoUtils;
import o2o.config.dao.utils.Expression;
import o2o.config.dao.utils.QueryCondition;
import o2o.dao.PO.PojoTables;
import o2o.dao.PO.user.USER_COOPERATE_APPLY_STATUS;
import o2o.dao.PO.user.UserCooperateApplyInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.Collections;
import java.util.List;

@Service
public class UserCooperateApplyService {


    /**
     * 通过用户账号获取用户的申请
     *
     * @param username
     * @param type
     * @return
     */
    public List<UserCooperateApplyInfo> findByUsernameAndType(String username, Integer type) {
        QueryCondition query = new QueryCondition();
        Expression expression = Expression.getInstance().addChild("delFlag", 0);
        if (StringUtils.isEmpty(username)) {
            expression.addChild("username", username);
        }
        if (ValidateUtil.isAbsInteger(type)) {
            expression.addChild("type", type);
        }
        query.addCondition(expression);
        String sql = DaoUtils.getSql(PojoTables.USER_COOPERATE_APPLY, query);

        List<UserCooperateApplyInfo> list = null;
        try {
            list = (List<UserCooperateApplyInfo>) UserDaoSql.getInstance().list(UserCooperateApplyInfo.class, sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ValidateUtil.isAbsList(list) ? list : Collections.emptyList();
    }

    /**
     * 添加或者修改申请
     *
     * @param applyInfo
     * @param opUser
     * @throws SQLException
     */
    public void saveOrCreateApplyInfo(UserCooperateApplyInfo applyInfo, String opUser) throws Exception {
        // 修改
        if (applyInfo.getId() != null) {
            if (applyInfo.getStatus() - USER_COOPERATE_APPLY_STATUS.APPLY != 0) {
                throw new Exception("当前状态下不可操作");
            }
            StringBuffer sql = new StringBuffer();
            sql.append(" update ").append(PojoTables.USER_COOPERATE_APPLY)
                    .append(" set updateUser ='").append(opUser).append("' ");
            if (StringUtils.isNotEmpty(applyInfo.getName())) {
                sql.append(" ,name ='").append(applyInfo.getName()).append("' ");
            }
            if (StringUtils.isNotEmpty(applyInfo.getTelPhone())) {
                sql.append(" ,telPhone ='").append(applyInfo.getTelPhone()).append("' ");
            }
            if (StringUtils.isNotEmpty(applyInfo.getTelPhone2())) {
                sql.append(" ,telPhone2 ='").append(applyInfo.getTelPhone2()).append("' ");
            }
            if (StringUtils.isNotEmpty(applyInfo.getEmail())) {
                sql.append(" ,email ='").append(applyInfo.getEmail()).append("' ");
            }
            if (StringUtils.isNotEmpty(applyInfo.getProvince())) {
                sql.append(" ,province ='").append(applyInfo.getProvince()).append("' ");
            }
            if (StringUtils.isNotEmpty(applyInfo.getCity())) {
                sql.append(" ,city ='").append(applyInfo.getCity()).append("' ");
            }
            if (StringUtils.isNotEmpty(applyInfo.getCounty())) {
                sql.append(" ,county ='").append(applyInfo.getCounty()).append("' ");
            }
            if (StringUtils.isNotEmpty(applyInfo.getNote())) {
                sql.append(" ,note ='").append(applyInfo.getNote()).append("' ");
            }
            sql.append(" where id = ").append(applyInfo.getId()).append(" and delFlag = 0");
            UserDaoSql.getInstance().executeBySql(sql.toString());
        } else {
            // 新增
            applyInfo.setCreateUser(opUser).setDelFlag(0).setStatus(USER_COOPERATE_APPLY_STATUS.APPLY)
                    .setCreateTime(DateUtils.getCurrTime());
            UserDaoSql.getInstance().saveObj(PojoTables.USER_COOPERATE_APPLY, applyInfo);
        }
    }

    /**
     * 获取申请合作列表
     *
     * @param city
     * @param county
     * @param type
     * @param status
     * @param params
     * @return
     */
    public List<UserCooperateApplyInfo> find(String city, String county, Integer type, Integer status, String params) {
        StringBuffer sql = new StringBuffer();
        sql.append(" select * from ").append(PojoTables.USER_COOPERATE_APPLY)
                .append(" where delFlag = 0 ");
        if (ValidateUtil.isNotNull(city)) {
            sql.append(" and city ='").append(city).append("' ");
        }
        if (ValidateUtil.isNotNull(county)) {
            sql.append(" and county ='").append(county).append("' ");
        }
        if (type != null) {
            sql.append(" and type =").append(type).append(" ");
        }
        if (status != null) {
            sql.append(" and status =").append(status).append(" ");
        }
        if (StringUtils.isNotEmpty(params)) {
            sql.append(" and (").append(" username like '%").append(params).append("%' or telPhone like '%").append(params).append("%')");
        }
        List<UserCooperateApplyInfo> list = null;
        try {
            list = (List<UserCooperateApplyInfo>) UserDaoSql.getInstance().list(UserCooperateApplyInfo.class, sql.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ValidateUtil.isAbsList(list) ? list : Collections.emptyList();
    }

    /**
     * 更新状态
     *
     * @param id
     * @param status
     * @param opUser
     * @throws SQLException
     */
    public void modifyStatus(Long id, int status, String opUser) throws SQLException {
        String sql = " update " + PojoTables.USER_COOPERATE_APPLY + " set status =" + status + " , updateUser ='" + opUser + "' where delFlag = 0 and id =" + id;
        UserDaoSql.getInstance().executeBySql(sql);
    }
}
